//
//  ImageCycly.m
//  yierya-teachers
//
//  Created by MacBook on 15/12/4.
//  Copyright © 2015年 Pro. All rights reserved.
//

#import "ImageCycly.h"
#import "UIImageView+WebCache.h"
#define kIdentifier @"WMLoopViewCellIdentifier"
#define kPageH 20
@interface ImageCycly() <UIScrollViewDelegate>
{
    NSArray *_titleArray;
    
    UILabel *_titleLabel;
    
    UIView *_numView;
    
    UILabel *_numLabel1;
    UILabel *_numLabel2;
    
}
@property (nonatomic, strong) NSMutableArray *currentImages;
@property (nonatomic,strong) NSMutableArray *currentTitle;

@property (nonatomic, assign) int currentPage;
@property (nonatomic, weak) UIPageControl *pageControl;
@property (nonatomic, weak) UIScrollView *scrollView;
@end

@implementation ImageCycly
- (id)initWithFrame:(CGRect)frame images:(NSMutableArray *)images autoPlay:(BOOL)isAuto delay:(NSTimeInterval)timeInterval titleAray:(NSMutableArray *)titles{
    if (self = [super initWithFrame:frame]) {
        _autoPlay = isAuto;
        _timeInterval = timeInterval;
        _images = images;
        _currentPage = 0;
        _titleArray = titles;
        [self addScrollView];
        [self addPageControl];
        
//        [self imgNumber]; //图片个数view
        
        if (self.autoPlay == YES) {
            [self toPlay];
        }
    }
    return self;
}

#pragma marke-图片个数位置
-(void)imgNumber{
    
    _numView = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width*0.9, 10, 30, 30)];
    _numView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
    
    _numView.layer.cornerRadius = 30/2;
    _numView.layer.masksToBounds = YES;
    [self addSubview:_numView];
    
    _numLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(3, 0, 18, 30)];
    _numLabel1.font = [UIFont fontWithName:@"HelveticaNeue" size:15];
    _numLabel1.textColor = [UIColor whiteColor];
    _numLabel1.text = @"1/";
    _numLabel1.textAlignment = NSTextAlignmentCenter;
    [_numView addSubview:_numLabel1];
    
    _numLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(18, 1, 7, 30)];
    _numLabel2.font = [UIFont fontWithName:@"HelveticaNeue" size:13];;
    _numLabel2.textColor = [UIColor whiteColor];
    _numLabel2.text = [NSString stringWithFormat:@"%ld",(unsigned long)_images.count];;
    _numLabel2.textAlignment = NSTextAlignmentCenter;
    [_numView addSubview:_numLabel2];
}
#pragma mark - Public Methods
- (void)addPageControl{
    CGFloat height = self.frame.size.height;
    CGFloat width = self.frame.size.width;
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, height-kPageH, width, kPageH)];
    //    bgView.backgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.2];
    bgView.backgroundColor = [UIColor clearColor];
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, width, kPageH)];
    
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, width-20, kPageH)];
    _titleLabel.text = _titleArray[_pageControl.currentPage];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [pageControl addSubview:_titleLabel];
    
    //个数
    pageControl.numberOfPages = _images.count;
    pageControl.currentPage = 0;//显示的默认位置
    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    pageControl.pageIndicatorTintColor = [UIColor grayColor];
    pageControl.userInteractionEnabled = NO;
    pageControl.clipsToBounds = NO;
    
    _pageControl = pageControl;
    [bgView addSubview:self.pageControl];
    [self addSubview:bgView];
}

#pragma mark - Private Methods
//定时操作
- (void)toPlay{
    [self performSelector:@selector(autoPlayToNextPage) withObject:nil afterDelay:_timeInterval];
}
- (void)autoPlayToNextPage{
    //取消前面的请求
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoPlayToNextPage) object:nil];
    
    [self.scrollView setContentOffset:CGPointMake(self.frame.size.width * 2, 0) animated:YES];
    
    [self performSelector:@selector(autoPlayToNextPage) withObject:nil afterDelay:_timeInterval];
}

//current:现在的
- (NSMutableArray *)currentImages{
    if (_currentImages == nil) {
        _currentImages = [[NSMutableArray alloc] init];
    }
    
    [_currentImages removeAllObjects];
    NSInteger count = self.images.count;
    int i = (int)(_currentPage + count - 1)%count;

    [_currentImages addObject:self.images[i]];
    [_currentImages addObject:self.images[_currentPage]];
    
    i = (int)(_currentPage + 1)%count;
    [_currentImages addObject:self.images[i]];
    return _currentImages;
}


- (NSMutableArray *)currentTitle{
    if (_currentTitle == nil) {
        _currentTitle = [[NSMutableArray alloc] init];
    }
    
    [_currentTitle removeAllObjects];
    NSInteger count = _titleArray.count;
    int i = (int)(_currentPage + count - 1)%count;
    [_currentTitle addObject:_titleArray[i]];
    [_currentTitle addObject:_titleArray[_currentPage]];
    
    i = (int)(_currentPage + 1)%count;
    [_currentTitle addObject:_titleArray[i]];
    return _currentTitle;
}


- (void)addScrollView{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    
    for (int i = 0; i < 3; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * width, 0, width, height)];
        
        [imageView sd_setImageWithURL:self.currentImages[i]];
        [scrollView addSubview:imageView];
    }
    scrollView.contentSize = CGSizeMake(3*width, height);
    scrollView.contentOffset = CGPointMake(width, 0);
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.delegate = self;
    
    [self addSubview:scrollView];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTapped:)];
//
    [scrollView addGestureRecognizer:tap];
    _scrollView = scrollView;
}


//更新图片
- (void)refreshImages{
    NSArray *subViews = self.scrollView.subviews;
    for (int i = 0; i < subViews.count; i++) {
        UIImageView *imageView = (UIImageView *)subViews[i];
        
        [imageView sd_setImageWithURL:self.currentImages[i]];
        
    }
    [self.scrollView setContentOffset:CGPointMake(self.frame.size.width, 0)];
}


#pragma mark - delegate图片点击事件的代理
- (void)singleTapped:(UITapGestureRecognizer *)recognizer{
    if ([self.delegate respondsToSelector:@selector(loopViewDidSelectedImage:index:)]) {
        [self.delegate loopViewDidSelectedImage:nil index:_currentPage];
    }
}

//Scroll卷轴
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat x = scrollView.contentOffset.x;
    CGFloat width = self.frame.size.width;
    if (x >= 2 * width) {
        _currentPage = (++_currentPage) % self.images.count;
        self.pageControl.currentPage = _currentPage;
        
        _titleLabel.text = _titleArray[_pageControl.currentPage];
        
        _numLabel1.text =[NSString stringWithFormat:@"%ld/",_pageControl.currentPage+1];
        
        [self refreshImages];
    }
    if (x <= 0) {
        _currentPage = (int)(_currentPage + self.images.count - 1)%self.images.count;
        self.pageControl.currentPage = _currentPage;
        
        _titleLabel.text = _titleArray[_pageControl.currentPage];
        
        _numLabel1.text =[NSString stringWithFormat:@"%ld/",_pageControl.currentPage+1];
        
        [self refreshImages];
    }
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [scrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:YES];
    
}

@end
